﻿using Common.AspNetCore;
using Common.Infrastructure;
using FCP.Api.Models;
using FCP.Api.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FCP.Api.Controllers
{
    [Route("api/FCPData")]
    public class FCPDataController : ApiAuthorizeController
    {
        private readonly FCPDataService dataService;
        public FCPDataController(IServiceProvider provider, FCPDataService auth)
             : base(provider)
        {
            this.dataService = auth;
        } 

        [HttpPost, Route("OrderPage")]
        public async Task<ApiActionResult<string>> OrderPage([FromBody] OrderPageRequest request)
        {
            return await SafeExecuteAsync<string>(async (param) =>
            {
                return await this.dataService.OrderPageByParam(request.ModelName, request.OrderPageParam);
            });
        }

        [HttpPost, Route("Get")]
        public async Task<ApiActionResult<string>> Get([FromBody] GetRequest request)
        {
            return await SafeExecuteAsync<string>(async (param) =>
            {
                return await this.dataService.GetByParam(request.EntityName, request.Id);
            });
        }


        [HttpPost, Route("Update")]
        public async Task<ApiActionResult<bool>> Update([FromBody] FullUpdateRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.dataService.FullUpdateByParam(request.EntityName, request.Data);
            });
        }

        [HttpPost, Route("Delete")]
        public async Task<ApiActionResult<bool>> Delete([FromBody] DeleteRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.dataService.DeleteByParam(request.EntityName, request.Id);
            });
        }
    }
}
